package com.yoga.manager.admin.impl;

import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.PlainVipBO;
import com.yoga.common.bo.RenewPlainVipBO;
import com.yoga.dao.dao.PlainVipDao;
import com.yoga.dao.domain.PlainVipDO;
import com.yoga.manager.admin.AdminPlainVipManager;
import com.yoga.manager.beanUtil.PlainVipBeanUtil;
import com.yoga.manager.uitl.DateStyle;
import com.yoga.manager.uitl.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by hdx on 2020/9/29.
 */
@Component
public class AdminPlainVipManagerImpl implements AdminPlainVipManager {
    Logger logger = LoggerFactory.getLogger(AdminPlainVipManagerImpl.class);

    @Resource
    private PlainVipDao plainVipDao;

    @Override
    public Boolean openPlainVip(PlainVipBO plainVipBO) throws AppException {
        if (plainVipBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (plainVipBO.getPlainVipId() == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        if (plainVipBO.getVipStartDate() == null || plainVipBO.getVipEndDate() == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (plainVipBO.getVipPrice() == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        Date today = DateUtil.dateFormat(new Date(), DateStyle.YYYY_MM_DD);
        if (plainVipBO.getVipStartDate() != null) {
            plainVipBO.setVipStartDate(DateUtil.dateFormat(plainVipBO.getVipStartDate(), DateStyle.YYYY_MM_DD));
        }
        if (plainVipBO.getVipEndDate() != null) {
            plainVipBO.setVipEndDate(DateUtil.dateFormat(plainVipBO.getVipEndDate(), DateStyle.YYYY_MM_DD));
        }
        if (plainVipBO.getVipStartDate().getTime() < plainVipBO.getVipEndDate().getTime()) {
            throw new AppException(AppErrorCodeEnum.START_DATE_BEFORE_END_DATE);
        }
        PlainVipDO plainVipDO = plainVipDao.findByYogaUserId(plainVipBO.getYogaUserId());
        if (plainVipDO == null) {
            throw new AppException(AppErrorCodeEnum.QUERY_DATA_NULL);
        }
        plainVipBO.setPlainVipId(plainVipDO.getId());
        int num = plainVipDao.updateById(PlainVipBeanUtil.plainVipBOToDO(plainVipBO));
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return Boolean.TRUE;
    }

    @Override
    public Boolean renewPlainVip(RenewPlainVipBO renewPlainVipBO) throws AppException {
        if (renewPlainVipBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (renewPlainVipBO.getPlainVipId() == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        if (StringUtils.isEmpty(renewPlainVipBO.getVipPrice())) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (renewPlainVipBO.getRenewType() == null) {
            throw new AppException(AppErrorCodeEnum.RENEW_TYPE_NULL);
        }

        PlainVipDO old = plainVipDao.findById(renewPlainVipBO.getPlainVipId());
        if (old == null) {
            throw new AppException(AppErrorCodeEnum.QUERY_DATA_NULL);
        }
        if (renewPlainVipBO.getRenewType() != old.getVipType()) {
            throw new AppException(AppErrorCodeEnum.RENEW_TYPE_ERROR);
        }
        PlainVipDO update = new PlainVipDO();
        update.setId(renewPlainVipBO.getPlainVipId());
        update.setTotalPrice((Double.parseDouble(old.getTotalPrice()) + Double.parseDouble(renewPlainVipBO.getVipPrice())) + "");
        update.setVipPrice(renewPlainVipBO.getVipPrice());
        int num = 0;
        if (renewPlainVipBO.getRenewType() == 1) {//团课会员续卡
            if (renewPlainVipBO.getMonthNum() == null || renewPlainVipBO.getMonthNum() <= 0) {
                throw new AppException(AppErrorCodeEnum.MONTH_NUM_NULL);
            }
            update.setVipEndDate(DateUtil.addMonth(DateUtil.dateFormat(old.getVipEndDate(), DateStyle.YYYY_MM_DD), renewPlainVipBO.getMonthNum()));
            num = plainVipDao.renewPlainVip(update);
        } else if (renewPlainVipBO.getRenewType() == 2) {//次卡会员续卡
            if (renewPlainVipBO.getClassNum() == null || renewPlainVipBO.getClassNum() <= 0) {
                throw new AppException(AppErrorCodeEnum.PARAM_ERROR);
            }
            update.setClassNum(old.getClassNum() + renewPlainVipBO.getClassNum());
            num = plainVipDao.renewPlainVip(update);
        } else {
            return false;
        }

        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean stopPlainVip(Long plainVipId, Integer month) throws AppException {
        if (plainVipId == null || month == null || month <= 0) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        PlainVipDO old = plainVipDao.findById(plainVipId);
        if (old == null) {
            throw new AppException(AppErrorCodeEnum.QUERY_DATA_NULL);
        }
        PlainVipDO plainVipDO = new PlainVipDO();
        plainVipDO.setId(plainVipId);
        plainVipDO.setStopEndDate(DateUtil.dateFormat(new Date(), DateStyle.YYYY_MM_DD));
        plainVipDO.setVipEndDate(DateUtil.addMonth(old.getVipEndDate(), month));
        int num = plainVipDao.updateById(plainVipDO);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return Boolean.TRUE;
    }

    @Override
    public Boolean updateData(PlainVipBO plainVipBO) throws AppException {
        if (plainVipBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (plainVipBO.getPlainVipId() == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        PlainVipDO plainVipDO = PlainVipBeanUtil.plainVipBOToDO(plainVipBO);
        int num = plainVipDao.updateById(plainVipDO);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return Boolean.TRUE;
    }

    @Override
    public Boolean cutClassNum(Long plainVipId) throws AppException {
        if (plainVipId == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        PlainVipDO old = plainVipDao.findById(plainVipId);
        if (old == null) {
            throw new AppException(AppErrorCodeEnum.QUERY_DATA_NULL);
        }
        if (old.getVipType() != 2) {
            throw new AppException(AppErrorCodeEnum.NOT_NUM_VIP);
        }
        if (old.getClassNum() <= 0) {
            throw new AppException(AppErrorCodeEnum.CLASS_NUM_0);
        }
        int num = plainVipDao.cutClassNum(plainVipId);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return Boolean.TRUE;
    }

    @Override
    public Boolean addClassNum(Long plainVipId) throws AppException {
        if (plainVipId == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        PlainVipDO old = plainVipDao.findById(plainVipId);
        if (old == null) {
            throw new AppException(AppErrorCodeEnum.QUERY_DATA_NULL);
        }
        if (old.getVipType() != 2) {
            throw new AppException(AppErrorCodeEnum.NOT_NUM_VIP);
        }

        int num = plainVipDao.addClassNum(plainVipId);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return Boolean.TRUE;
    }

    @Override
    public PlainVipBO getByUserId(Long yogaUserId) throws AppException {
        if (yogaUserId == null) {
            throw new AppException(AppErrorCodeEnum.YOGA_USER_ID_NULL);
        }
        PlainVipBO plainVipBO = PlainVipBeanUtil.plainVipDOToBO(plainVipDao.findByYogaUserId(yogaUserId));


        return plainVipBO;
    }
}
